// pages/buyMenberCard/buyMenberCard.js
var app = getApp()
Page({
  data: {
    current:0,
    showCardIntroduce: false,  //显示会员卡权益和使用说明
    currentCard:{},
    payType: '1',
    hadCardRight:true,
    hadCardNotice: true,
  },
  onLoad: function (e) {
    var that = this;
    app.setVersion(that);
    wx.getSystemInfo({
      success: function(res) {
        console.log(res)
        var x = res.windowWidth - 80;
        var y = res.windowHeight - 70;
        that.setData({
          x: x,
          y: y,
          showBtn:true
        })
      },
    })
  },
  onShow: function () {
    var that = this;
    app.setNavtitle('会员卡');
    if (!app.globalData.plumSession) {
      app.plumSessionCallback = plumSession => {
        if (plumSession != '') {
          console.log('查询到登录信息');
          that.requestCardList();
          that.getCurrentDate();
          that.getUserInfor();
        } else {
          console.log('未查询到登录信息');
          app.wechatSq(that);
        }
      }
    } else {
      that.requestCardList();
      that.getCurrentDate();
      that.getUserInfor();
    }
  },
  requestCardList: function (cid) {
    
    var that = this;
    var data = {};
    data.map = 'applet_member_card_list';
    data.type = 2;
    app.setVersion(that);
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          that.setData({
            cardList: res.data.data,
            currentCard: res.data.data[that.data.current]
          })
        } else {
          that.setData({
            cardList: []
          })
        }
      },
      complete: function () {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    });
  },
  toggleHadCardRight(){
    this.setData({
      hadCardRight: !this.data.hadCardRight
    })
  },
  toggleHadCardNotice(){
    this.setData({
      hadCardNotice: !this.data.hadCardNotice
    })
  },
  //获得当前的日期
  getCurrentDate: function () {
    var that = this;
    var myDate = new Date();
    //获取当前年
    var year = myDate.getFullYear();
    //获取当前月
    var month = myDate.getMonth() + 1;
    //获取当前日
    var day = myDate.getDate();
    var str = year + '-' + month + '-' + day;
    that.setData({
      date: str
    });
    console.log(that.data.date);
  },
  //获得所填信息
  getUserInfor: function () {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_store_member_info',
      },
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data);
          that.setData({
            name: res.data.data.name,
            sexValue: res.data.data.gender ? res.data.data.gender : '1',
            date: res.data.data.birthday,
            telphone: res.data.data.telphone,
            coin: res.data.data.coin
          })
        } else {
          console.log(res.data)
        }
      },
      complete: function () {
        // wx.hideLoading();
      }
    });
  },
  //获得生日
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  //获得支付方式
  radioChange: function (e) {
    var that = this;
    console.log('radio发生change事件，携带value值为：', e.detail.value);
    that.setData({
      payType: e.detail.value
    })
  },
  sexSelect: function (e) {
    var that = this;
    var sex = e.currentTarget.dataset.sex;
    that.setData({
      sexValue: sex
    })
  },
  nameChange: function (e) {
    var that = this;
    that.setData({
      name: e.detail.value
    })
    console.log(that.data.name);
  },
  phoneChange: function (e) {
    var that = this;
    console.log('电话');
    console.log(e.detail.value);
    that.setData({
      telphone: e.detail.value
    })
    console.log(that.data.telphone);
  },
  submitInfor: function () {
    var that = this;
    app.getSubId(that, 'applet_buy_member_card', 'toSubmitInfor');
  },
  toSubmitInfor: function () {
    var that = this;
    if (!that.data.currentCard.levelCanBuy){
      app.errorTip(that, "不能降级购买会员卡", 2000);
      return
    }
    var data = {};
    var id = that.data.currentCard.id;
    var type = that.data.currentCard.type;
    var name = that.data.name;
    var gender = that.data.sexValue;
    var birthday = that.data.date;
    var telphone = that.data.telphone;
    var payType = that.data.payType;
    data.map = 'applet_buy_member_card';
    data.cardid = id;
    data.type = type;
    data.name = name;
    data.gender = gender;
    data.birthday = birthday;
    data.telphone = telphone;
    data.payType = payType;
    console.log(data);
    if (!data.telphone) {
      app.errorTip(that, "请输入手机号码", 2000);
      return;
    }
    wx.showModal({
      title: '提示',
      content: '确认立即支付吗？',
      success(result) {
        if (result.confirm) {
          wx.showLoading({
            title: '加载中',
          })
          wx.request({
            url: app.globalData.requestUrl,
            data: data,
            success: function (res) {
              if (res.data.ec == 200) {
                console.log(res.data.data);
                if (res.data.data.status == 'dzf') {
                  that.orderPay(res.data.data.tid);
                } else if (res.data.data.status == 'zfcg') {
                  wx.showToast({
                    icon:'success',
                    title: '支付成功',
                    success(){
                      setTimeout(function () {
                        wx.navigateBack({
                          delta: 1
                        })
                      }, 1500)
                    }
                  })
                  
                }
              } else {
                console.log(res.data);
                wx.hideLoading();
                app.errorTip(that, res.data.em, 2000);
              }
            },
            complete: function () {
              // wx.hideLoading();
            }
          });
        } else if (result.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  orderPay: function (tid) {
    var that = this;
    var data = {};
    data.map = 'applet_pay_member_card';
    data.tid = tid;
    console.log(data);
    //发起请求，获取列表列表
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true,
      duration: 1000
    });
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          wx.requestPayment({
            'appId': res.data.data.appId,
            'timeStamp': res.data.data.timeStamp,
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package,
            'signType': res.data.data.signType,
            'paySign': res.data.data.paySign,
              success: function (res) {
              wx.showToast({
                icon: 'success',
                title: '支付成功',
                success() {
                  setTimeout(function () {
                    wx.navigateBack({
                      delta: 1
                    })
                  }, 1500)
                },
              })
            },
            fail: function (res) {
              wx.showToast({
                icon: 'none',
                title: '支付失败',
              })
              console.log(res); 
            }
          });
        } else {
          wx.hideToast();
          console.log(res.data);
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        
      }
    });
  },
  openMemberRecord:function(){
    var that =  this;
    wx.navigateTo({
      url: '/subpages/memberRecord/memberRecord',
    })
  },
  // 会员卡介绍
  openCardIntroduce() {
    this.setData({
      showCardIntroduce: true
    })
  },
  closeCardIntroduce() {
    this.setData({
      showCardIntroduce: false
    })
  },
  //得到当前swiper的current值
  getCurrent:function(e){
    var that = this;
    console.log(e.detail);
    that.setData({
      current: e.detail.current,
      currentCard: this.data.cardList[e.detail.current]
    });
  },
  changeCurrent:function(e){
    var that = this;
    var type = e.currentTarget.dataset.type;
    var current = that.data.current;
    if(type=='prev'){
      if (current>0){
        current--;
      }
    }else if(type=='next'){
      if (current < that.data.cardList.length - 1){
        current++;
      }
    }
    that.setData({
      current: current,
      currentCard: this.data.cardList[e.detail.current]
    })
  },
  prevCard:function(){
    var that = this;
    var current = that.data.current;
    if (current==0){
      current = 1
    }else{
      current--;
    }
    console.log(current)
    that.setData({
      current: current
    });
  },
  nextCard: function () {
    var that = this;
    var current = that.data.current;
    if (current == 1) {
      current = 0
    }else{
      current++;
    }
    console.log(current)
    that.setData({
      current: current
    });
  },
  //打开支付订单页面
  // openMemberCardPay:function(e){
  //   var that = this;
  //   var id = e.currentTarget.dataset.id;
  //   var cardName = e.currentTarget.dataset.name;
  //   var price = e.currentTarget.dataset.price;
  //   var type = e.currentTarget.dataset.type;
  //   console.log(id)
  //   console.log(cardName)
  //   console.log(price)
  //   wx.navigateTo({
  //     url: '/subpages/memberCardPay/memberCardPay?id=' + id + '&cardName=' + cardName+'&price='+price+'&type='+type,
  //   })
  // },
})